﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entities;
using BLL;
namespace Stationery.UC
{
    public partial class UC_User_Update : System.Web.UI.UserControl
    {
        User user = new User();
        BLL_User bll_User = new BLL_User();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Params["status"] == "edit")
                {
                    Int64 intID = Convert.ToInt64(Request.Params["id_user"].ToString());
                    user = bll_User.Lst_User(bll_User.Get_User(intID))[0];

                    ASPxTextBoxConfirm.DisabledStyle.BackColor = System.Drawing.Color.Gray;
                    ASPxTextBoxPassword.DisabledStyle.BackColor = System.Drawing.Color.Gray;

                    ASPxTextBoxUsername.Text = user.USERNAME;
                    ASPxTextBoxFullName.Text = user.FULLNAME;
                    ASPxTextBoxEmail.Text = user.EMAIL;
                    ASPxTextBoxEmailSkype.Text = user.SKYPE;
                    ASPxTextBoxMobile.Text = user.MOBILE;
                    ASPxTextBoxPassword.Value = EncPass.DecryptString(user.PASSWORD, Utils.SHARE_SECRETKEY);
                    ASPxTextBoxConfirm.Text = EncPass.DecryptString(user.PASSWORD, Utils.SHARE_SECRETKEY);
                    ASPxTextBoxPassword.Enabled = false;
                    ASPxTextBoxConfirm.Enabled = false;
                    ASPxTextBoxPassword.ReadOnly = true;
                    ASPxTextBoxConfirm.ReadOnly = true;
                    RequiredFieldValidator3.Enabled = false;
                    RequiredFieldValidator4.Enabled = false;
                    CompareValidator1.Enabled = false;
                }
            }
        }

        protected void ASPxButtonSave_Click(object sender, EventArgs e)
        {
            string strStatus = "";
            Int32 intID;
            user.USERNAME = ASPxTextBoxUsername.Text;
            user.FULLNAME = ASPxTextBoxFullName.Text;
            user.EMAIL = ASPxTextBoxEmail.Text;
            user.SKYPE = ASPxTextBoxEmailSkype.Text;
            user.MOBILE = ASPxTextBoxMobile.Text;
            user.PASSWORD = EncPass.EncryptString(ASPxTextBoxPassword.Text, Utils.SHARE_SECRETKEY);
            if (Request.Params["status"] != null)
            {
                strStatus = Request.Params["status"].ToString();
                if (Request.Params["status"] == "edit")
                {
                    intID = Convert.ToInt32(Request.Params["id_user"].ToString());
                    user.ID = intID;

                    // Kiem tra user nay da ton tai chua, neu ton tai thi thong bao
                    if (bll_User.Get_User_Dynamic(String.Format("USERNAME={0} AND ID<>{1}", user.USERNAME, user.ID)).Rows.Count > 0)
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "message", "alert('User đã tồn tại, nhập user khác');", true);
                        return;
                    }
                }
                else
                {
                    if (bll_User.Get_User_Dynamic(String.Format("USERNAME={0}", user.USERNAME, user.ID)).Rows.Count > 0)
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "message", "alert('User đã tồn tại, nhập user khác');", true);
                        return;
                    }
                }
            }


            if (bll_User.InsertUpdate_User(user))
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "message", "alert('Thành công');location.href = 'Admin.aspx?id=user';", true);
            }
        }

        protected void ASPxButtonCancel_Click(object sender, EventArgs e)
        {
            Session[Utils.SESSION_USER] = null;
            Response.Redirect(UtilsURL.user);
        }

        protected void ASPxButtonBack_Click(object sender, EventArgs e)
        {
            Response.Redirect(UtilsURL.user);
        }
    }
}